import { baseApiUrl, path, backgruondMusic, successMusic } from "../../utils/config";
import { upload, stand, standPost } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,

    whether: false,
    date: "00:00:00",

    tempFilePath: null,

    number: 0,
    // height: 300,
    height: 642,
    timer: null,

    show: false,
    minDate: new Date(2023, 8, 1).getTime(),
    maxDate: new Date().getTime(),
    defaultDate: app.globalData.defaultDate,
    formatter: (day) => {
      const date = day.date;
      let y = date.getFullYear();
      let m = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      let d = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      let time = `${y}-${m}-${d}`;
      const defaultDate = app.globalData.defaultDate;

      if (defaultDate.includes(time)) {
        let list = app.globalData.list;
        day.bottomInfo = list[time];
      }
      return day;
    },
    list: [
      { name: "1分钟", duration: 1 },
      { name: "3分钟", duration: 3 },
      { name: "5分钟", duration: 5 },
      { name: "10分钟", duration: 10 },
      { name: "15分钟", duration: 15 },
      { name: "30分钟", duration: 30 },
      { name: "60分钟", duration: 60 }
    ],
    duration: 1,
    value: 1 * 60 * 1000,
    timeData: {},
    have: false,
    status: false,
    timer: null,

    backgroundAudio: null,
    successAudio: null
  },
  onLoad() {
    const date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth();
    let minDate = new Date(y, m, 1).getTime();
    this.setData({ minDate });
  },
  onShow() {
    this.getDeta();
  },
  async getDeta() {
    let res = await stand();
    let { all_day } = res;
    let status = res.stand?.status;
    status = status == 0 ? true : false;
    // this.setData({ number: all_day, status });

    // let number = this.data.number;//已打卡天数
    // let total = 49;//需要打卡的天数
    // this.timer = setInterval(() => {
    //   number += 1;
    //   number = number > 49 ? 49 : number;
    //   let result = total - number;
    //   let height = Math.ceil(result / total * 642);
    //   console.log(height)
    //   this.setData({ height, number });

    //   if (number == 49) {
    //     clearInterval(this.timer);
    //   }
    // }, 500);

    let number = all_day;//已打卡天数
    let total = 49;//需要打卡的天数
    number = number > 49 ? 49 : number;
    let result = total - number;
    let height = Math.ceil(result / total * 642);
    // this.setData({ height, number, status });
    this.setData({ height, number: all_day, status });
  },
  onHide() {
    this.destroyMusic();
    let countDown = this.selectComponent('#count-down');
    if (!countDown) return false;
    countDown.pause();
    clearInterval(this.timer);
    this.setData({ number: 0, height: 642, have: false });
  },
  onUnload() {
    let countDown = this.selectComponent('#count-down');
    if (!countDown) return false;
    countDown.pause();
    clearInterval(this.timer);
    this.setData({ number: 0, height: 642, have: false });
  },
  showCalendar() {
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false, whether: false });
  },
  change() {
    this.setData({ whether: true });
  },
  changeTime(e) {
    let { duration } = e.currentTarget.dataset;
    this.setData({ duration });
  },
  downChange(e) {
    let timeData = e.detail;
    this.setData({ timeData });
    let { minutes, seconds } = timeData;
    if (minutes == 0 && seconds == 10) {
      this.successAudio.play();
    }
  },
  finished(e) {
    let { duration, timer } = this.data;
    if (timer) clearTimeout(timer);
    timer = setTimeout(async () => {
      await standPost({ duration });
      Toast.success('站桩成功');
      this.setData({ have: false });
      this.destroyMusic();
      this.getDeta();
    }, 2000);
    this.setData({ timer, value: 0 });
  },
  async submit() {
    this.destroyMusic();
    let { duration } = this.data;
    let value = duration * 60 * 1000;
    this.setData({ value });
    this.createMusic();
    let countDown = this.selectComponent('#count-down');
    if (!countDown) return false;
    countDown.start();
    this.setData({ whether: false, have: true });
    wx.setKeepScreenOn({
      keepScreenOn: true
    });
  },
  createMusic() {
    this.backgroundAudio = wx.createInnerAudioContext();
    this.backgroundAudio.src = backgruondMusic;
    this.backgroundAudio.loop = true;
    this.backgroundAudio.play();
    this.backgroundAudio.onError(() => {
      Toast.success('播放站桩音乐失败');
    });
    this.successAudio = wx.createInnerAudioContext();
    this.successAudio.src = successMusic;
    this.successAudio.onError(() => {
      Toast.success('播放站桩结束音乐失败');
    });
  },
  destroyMusic() {
    if (this.backgroundAudio) {
      this.backgroundAudio.stop();
      this.backgroundAudio.destroy();
      this.backgroundAudio = null;
    };
    if (this.successAudio) {
      this.successAudio.stop();
      this.successAudio.destroy();
      this.successAudio = null;
    };
    wx.setKeepScreenOn({
      keepScreenOn: false
    });
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})